package algotithm.weekendrace.春季赛.test3;

import java.util.PriorityQueue;

/**
 * @author zhouyanxiang
 * @Date 2021-04-2021/4/5-17:36
 */
public class Solution {
    public int magicTower(int[] nums) {
        int len = nums.length;
        long last = 0;
        long sum = 1;
        int res = 0;
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((x, y) -> y - x);
        for (int i = 0; i < len; i++) {
            if (nums[i] >= 0) {
                sum += nums[i];
            } else {
                priorityQueue.add(-nums[i]);
                if (-nums[i] > sum) {
                    Integer poll = priorityQueue.poll();
                    res++;
                    sum += poll;
                    last += poll;
                }
                sum += nums[i];
            }
        }
        if (sum <= last) {
            res = -1;
        }
        return res;
    }
}
